//
//  UserInfoCell.m
//  PigMarket
//
//  Created by Felix on 2019/3/14.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "UserInfoCell.h"

#import "UIImageView+WebCache.h"


#define UserInfoCellID @"UserInfoCellID"

@interface UserInfoCell ()



/**
 titleLabel
 */
@property (nonatomic , strong) UILabel *titleLabel;

/**
 detailLabel
 */
@property (nonatomic , strong) UILabel *detailLabel;



/**
 gtImageView
 */
@property (nonatomic , strong) UIImageView *gtImageView;

@end

@implementation UserInfoCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    UserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:UserInfoCellID];
    if(!cell)
    {
        cell = [[UserInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:UserInfoCellID];
    }
    return cell;
}


#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 关闭cell点击效果
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 0)];
        // 初始化UI
        [self initView];
        // 布局
        [self initLayout];
    }
    return self;
}


- (void)initView {

    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLabel];

    [self.contentView addSubview:self.detailIamgeView];
    [self.contentView addSubview:self.detailLabel];
    [self.contentView addSubview:self.gtImageView];
}

- (void) initLayout {

    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(10);
    }];
    [self.gtImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];
    [self.detailIamgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(self.gtImageView.mas_left).offset(-10);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(self.gtImageView.mas_left).offset(-10);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(5);
    }];
    
    
    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.detailLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.gtImageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.detailLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
}


- (void)configWithType:(NSString *)type infoDic:(NSDictionary *)infoDic {
    
    
    if ([infoDic[@"icon"] length] > 0) {
        self.iconImageView.hidden = NO;
        self.iconImageView.image = [UIImage imageNamed:infoDic[@"icon"]];
        
        [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.height.width.mas_equalTo(34);
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(10);
        }];

        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(self.iconImageView.mas_right).offset(10);
        }];
        
    } else {
        self.iconImageView.hidden = YES;
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(20);
        }];
    }
    
    
    NSString *tempTitle = infoDic[@"title"];
    if ([tempTitle hasSuffix:@"*"]) {
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:tempTitle];
        text.yy_font = [UIFont largerFont];
        text.yy_color = [UIColor textBlackColor];
        NSRange range = NSMakeRange(text.length - 1, 1);
        [text yy_setColor:[UIColor redColor] range:range];
        self.titleLabel.attributedText = text;
    } else {
        self.titleLabel.text = tempTitle;
    }
    
    
    if ([infoDic[@"title"] isEqualToString:@"头像"]) {
        self.detailLabel.hidden = YES;
        self.detailIamgeView.hidden = NO;
        
        if ([JTool iSNull:infoDic[@"detail"]]) {
            self.detailIamgeView.image = [UIImage imageNamed:@"defaultheadericon_woman"];

        } else {
            [self.detailIamgeView sd_setImageWithURL:[NSURL URLWithString:infoDic[@"detail"]] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
        }
    } else {
        self.detailLabel.hidden = NO;
        self.detailIamgeView.hidden = YES;
        self.detailLabel.text = infoDic[@"detail"];
    }
}


#pragma mark - getter and setter

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
    }
    return _iconImageView;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont largerFont];
    }
    return _titleLabel;
}


- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = UIColorFromRGB(0x999999);
        _detailLabel.font = [UIFont largerFont];
        _detailLabel.textAlignment = NSTextAlignmentRight;
    }
    return _detailLabel;
}


- (UIImageView *)detailIamgeView {
    if (!_detailIamgeView) {
        _detailIamgeView = [[UIImageView alloc] init];
        _detailIamgeView.layer.cornerRadius = 30;
        _detailIamgeView.layer.masksToBounds = YES;
    }
    return _detailIamgeView;
}


- (UIImageView *)gtImageView {
    if (!_gtImageView) {
        _gtImageView = [[UIImageView alloc] init];
        _gtImageView.image = [UIImage imageNamed:@"user_gt"];
    }
    return _gtImageView;
}

@end
